package com.fuhao.springbootactivemq.comsumer;

import com.fuhao.springbootactivemq.Model.User;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

@Component
public class TopicListener2 {

    @Transactional
    @JmsListener(destination = "topic1",containerFactory = "topicListenerFactory")
    public void receiveQueue(Message message, Session session)
            throws JMSException {
        System.out.println("目的地：topic1，消费者2进来消费");
        try {
            ObjectMessage objectMessage = (ObjectMessage) message;
            User user = (User) objectMessage.getObject();
            System.out.println(user.toString());
            //int i = 1/0;//测试重试机制是否成功
            message.acknowledge();// 使用手动签收模式，需要手动的调用，如果不在catch中调用session.recover()消息只会在重启服务后重发
        } catch (Exception e) {
            session.recover();//重发消息
        }
    }
}
